// pages/epi_4/recordPlay/recordPlayWork.js
var timer = require('../../../utils/timer.js')

Page({
  data: {
    tempFilePath: null,
    time: '00:00:00',
    msg: {
      recordStart: '等待...',
      recordPause: '等待...',
      recordResume: '等待...',
      recordStop: '等待...',
      audioCanplay: '等待...',
      audioPlay: '等待...',
      audioPause: '等待...',
      audioStop: '等待...'
    },
    play: {
      currentTime: '00:00',
      duration: '00:00',
      percent: 0,
    },
  },
  audioctx: null,
  rec: null,

  onLoad(){
      this.audioCtx=wx.createInnerAudioContext()
      this.rec=wx.getRecorderManager()
  },

  // 录音控制
  recordStart: function(){
  },

  recordPause: function(){
  },

  recordResume: function(){
  },

  recordStop: function () {
  },

  // 音频控制
  audioPlay: function(){
  },
  audioPause: function(){
  },
  audioSeek: function(){
  },
  audioStop: function(){
  },

  onReady: function(){
    var that = this

    // 录音事件，注意在触发stop事件后，要将录音文件的路径保存下来
    this.rec.onStart(function(){
    })

    this.rec.onStop(res => {
      
    })

    this.rec.onPause(function(){
      
    })

    this.rec.onResume(function(){
      
    })

    this.audioCtx.onCanplay(function(){
      
    })
    this.updateState()

    this.audioCtx.onPlay(function(){

    })

    this.audioCtx.onPause(function(){
    })

    this.audioCtx.onStop(function(){
      
    })

  },

  updateState: function(){
      var updateTime = 0
      var that = this
      this.audioCtx.onTimeUpdate(() =>{
        var currentTime = parseInt(that.audioCtx.currentTime)
        if (!that.sliderChangeLock && currentTime !== updateTime){
          updateTime = currentTime
          that.setData({
            'play.duration': timer.convert(that.audioCtx.duration || 0),
            'play.currentTime': timer.convert(currentTime),
            'play.percent': currentTime / that.audioCtx.duration * 100
          })
        }
      })
    },

    sliderChangeLock: false,
    sliderChanging: function(e){
      var second = e.detail.value * this.audioCtx.duration / 100
      this.sliderChangeLock = true
      this.setData({
        'play.currentTime': timer.convert(second),
      })
    },
    sliderChange: function(e){
      var second = e.detail.value * this.audioCtx.duration / 100
      this.audioCtx.seek(second)
      setTimeout(()=>{
        this.sliderChangeLock = false
      }, 1000)
    },

})